package jmu.cultural_tourism.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jmu.cultural_tourism.entity.PostImage;
import jmu.cultural_tourism.service.PostImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * 图片表(PostImage)表控制层
 *
 * @author makejava
 * @since 2025-07-17 15:34:54
 */
@RestController
@Tag(name="帖子对应图像接口")
@RequestMapping("postImage")
public class PostImageController {
    /**服务对象
     */
    @Autowired
    private PostImageService postImageService;

    @GetMapping
    public ResponseEntity<Page<PostImage>> queryByPage(PostImage postImage, PageRequest pageRequest) {
        return ResponseEntity.ok(this.postImageService.queryByPage(postImage, pageRequest));
    }

    @GetMapping("{id}")
    public ResponseEntity<PostImage> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.postImageService.queryById(id));
    }

    /*通过帖子ID查询图像数据*/
    @PostMapping({"findByPostId/{pageNo}/{pageSize}", "findByPostId/{pageNo}", "findByPostId"})
    @Operation(summary = "通过帖子ID查询对应图像")
    public ResponseEntity<List<PostImage>> queryByPostId(
            @RequestParam Integer postId,
            @PathVariable(value = "pageNo",required = false) Integer pageNo,
            @PathVariable(value = "pageSize",required = false) Integer pageSize
    ) {

        if (pageNo == null) {
            pageNo = 0; // 第一页
        } else {
            pageNo = pageNo - 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        return ResponseEntity.ok(this.postImageService.queryByPostId(postId, pageRequest));
    }

    @PostMapping("add")
    @Operation(summary = "新增图像")
    public ResponseEntity<PostImage> add(@RequestBody PostImage postImage) {
        return ResponseEntity.ok(this.postImageService.insert(postImage));
    }

    /*编辑数据*/
    @PutMapping("edit")
    @Operation(summary = "修改图像")
    public ResponseEntity<PostImage> edit(@RequestBody PostImage postImage) {
        return ResponseEntity.ok(this.postImageService.update(postImage));
    }

    /*删除数据*/
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.postImageService.deleteById(id));
    }

}

